package com.wu.controller;

import com.wu.pojo.model.CommentModel;
import com.wu.response.CommonReturnType;
import com.wu.service.CommentManagementService;
import com.wu.service.CommentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wu.constant.Constant.*;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-04-28 16:59
 */
@RestController
@Api(tags = "评论")
@RequestMapping(value = "/comment", method = RequestMethod.POST)
public class CommentController {

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private CommentManagementService commentManagementService;

    @ApiOperation("文章评论审核功能设置，默认为false")
    @GetMapping("/tokenOpenOrClose")
    public CommonReturnType tokenOpenOrClose() {
        boolean ok = commentManagementService.tokenOpenOrClose();
        return CommonReturnType.create(ok);
    }
    @ApiOperation("获取文章评论审核功能设置，默认为false")
    @GetMapping("/tokenGetOpenOrClose")
    public CommonReturnType tokenGetOpenOrClose() {
        boolean ok = commentManagementService.tokenGetOpenOrClose();
        return CommonReturnType.create(ok);
    }


    /**
     * 用户进行评论
     *
     * @param type
     * @param articleId
     * @param parentCommentId
     * @param commentContent
     * @return
     */
    @ApiOperation(value = "用户进行评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = " 评论类型 如果是直接评论传 blog ,如果是回复传 user", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "articleId", value = "文章ID", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "parentCommentId", value = "父评论ID", dataTypeClass = String.class),
            @ApiImplicitParam(name = "commentContent", value = "评论内容", required = true, dataTypeClass = String.class)
    })
    @RequestMapping(value = "/addComment", method = RequestMethod.POST)
    public CommonReturnType tokenAddComments(@RequestParam(name = "type") String type,
                                             @RequestParam(name = "articleId") String articleId,
                                             @RequestParam(name = "parentCommentId", required = false) String parentCommentId,
                                             @RequestParam(name = "commentContent") String commentContent) {
        Map<String, Object> map = commentsService.addComment(type, articleId, parentCommentId, commentContent);
        return CommonReturnType.create(map);
    }

    /**
     * 根据文章ID获取评论
     *
     * @param articleId
     * @return
     */
    @ApiOperation(value = "根据文章ID获取评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId", value = "文章Id", required = true, dataTypeClass = String.class),

    })
    @RequestMapping(value = "/getComments", method = RequestMethod.POST)
    public CommonReturnType getCommentsByArticleId(@RequestParam(name = "articleId", defaultValue = DEFAULT_NULL_VALUE) String articleId) {
        List<CommentModel> allCommentByArticleId = commentsService.getAllCommentByArticleId(articleId);
        HashMap<Object, Object> map = new HashMap<>();
        map.put(COMMENTS, allCommentByArticleId);
        return CommonReturnType.create(map);
    }

    /**
     * 根据评论Id删除此评论及其子评论
     *
     * @param commentId
     * @return
     */
    @ApiOperation(value = "根据评论Id删除此评论及其子评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论Id", required = true, dataTypeClass = String.class),

    })
    @RequestMapping(value = "/deleteComments", method = RequestMethod.POST)
    public CommonReturnType tokenDeleteCommentsById(@RequestParam("commentId") String commentId) {
        Map<String, Object> map = commentsService.deleteCommentsByCommentId(commentId);
        return CommonReturnType.create(map);
    }


    /**
     * 获取自己文章的评论
     *
     * @return
     */
    @ApiOperation(value = "获取自己文章的评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", defaultValue = ONE, dataTypeClass = String.class),
    })
    @RequestMapping(value = "/getOwnerArticleComments", method = RequestMethod.POST)
    public CommonReturnType tokenGetOwnerArticleComments(@RequestParam(name = "curPage", defaultValue = ONE, required = false) Integer curPage) {
        Map<String, Object> map = commentManagementService.getCommentManagementByUserId(curPage);
        return CommonReturnType.create(map);
    }

    /**
     * 获取自己的评论
     *
     * @return
     */
    @ApiOperation(value = "获取自己的评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", defaultValue = ONE, dataTypeClass = String.class),
    })
    @RequestMapping(value = "/getOwnerComments", method = RequestMethod.POST)
    public CommonReturnType tokenGetOwnerComments(@RequestParam(name = "curPage", defaultValue = ONE, required = false) Integer curPage) {
        Map<String, Object> map = commentManagementService.getOwnerComments(curPage);
        return CommonReturnType.create(map);
    }


    /**
     * 通过审核
     *
     * @param commentId
     * @return
     */
    @ApiOperation(value = "通过审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论Id", required = true, dataTypeClass = String.class),
    })
    @RequestMapping(value = "/passComments", method = RequestMethod.POST)
    public CommonReturnType tokenPassComments(@RequestParam("commentId") String commentId) {
        commentsService.passCommentByCommentId(commentId);
        return CommonReturnType.create(null);
    }

    /**
     * 不通过审核
     *
     * @param commentId
     * @return
     */
    @ApiOperation(value = "不通过审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论Id", required = true, dataTypeClass = String.class),
    })
    @RequestMapping(value = "/notPassComments", method = RequestMethod.POST)
    public CommonReturnType tokenNotPassComments(@RequestParam("commentId") String commentId) {
        commentsService.notPassCommentByCommentId(commentId);
        return CommonReturnType.create(null);
    }


    /**
     * 获取待审核的评论
     *
     * @param curPage
     * @return
     */
    @ApiOperation(value = "获取待审核的评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", defaultValue = ONE, dataTypeClass = String.class)
    })
    @RequestMapping(value = "/getUserCommentWithNotPass", method = RequestMethod.POST)
    public CommonReturnType tokenGetUserCommentWithNotPass(@RequestParam(name = "curPage", defaultValue = ONE) Integer curPage) {
        Map<String, Object> map = commentManagementService.getAllNotPassCommentsByUserId(curPage);
        return CommonReturnType.create(map);
    }

    /**
     * 获取用户的最新评论
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取用户的最新评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", dataTypeClass = String.class)
    })
    @RequestMapping(value = "/getTheNewComments", method = RequestMethod.POST)
    public CommonReturnType getTheNewComments(@RequestParam("userId") String userId) {
        Map<String, Object> map = commentsService.getNewComments(userId);
        return CommonReturnType.create(map);
    }
}
